package com.txy.webmanagement.service;

import com.gitee.sunchenbin.mybatis.actable.manager.common.BaseMysqlCRUDManager;
import com.txy.webmanagement.dto.SysUserDTO;
import com.txy.webmanagement.mapper.SysUserMapper;
import com.txy.webmanagement.model.SysUser;
import com.txy.webmanagement.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class SysUserService {
    @Resource
    private SysUserMapper sysUserMapper;
    @Autowired
    private BaseMysqlCRUDManager baseMysqlCRUDManager;

    public SysUser getSysUserByUserName(String userName) {
        List<SysUser> uslist = sysUserMapper.getSysUserByUserName(userName);
        if (uslist != null && uslist.size() > 0) {
            return uslist.get(0);
        } else {
            return null;
        }
    }

    /**
     * 注册
     *
     * @param user
     * @return
     */
    public RequestDataUtils register(SysUserDTO user) {
        RequestDataUtils res = new RequestDataUtils();
        if (StringUtils.checkStringNotNull(user.getUserName()) && StringUtils.checkStringNotNull(user.getPassword()) && StringUtils.checkStringNotNull(user.getNickName())) {
            //验证用户是否存在
            SysUser useMd = getSysUserByUserName(user.getUserName());
            if (useMd != null) {
                res.setCode(SysParrentUtils.errorCode);
                res.setMessage("已存在邮箱账号，请更换新的邮箱，或通过邮箱找回密码！");
                return res;
            } else {
                SysUser us = new SysUser();
                us.setUuid(StringUtils.getUUID());
                us.setTypeId(SysParrentUtils.appUserTypeID);
                us.setNickName(user.getNickName());
                String passMd = Md5Utils.getResult(user.getPassword());
                us.setUserName(user.getUserName());
                us.setPassword(passMd);
                us.setEnable(1);
                Date dt = new Date();
                us.setCreateTime(dt);
                int ii = sysUserMapper.add(us);
                if (ii > 0) {
                    res.setCode(SysParrentUtils.successCode);
                    res.setMessage("新增成功！");
                } else {
                    res.setCode(SysParrentUtils.errorCode);
                    res.setMessage("增加用户数据失败！");
                }
            }
        } else {
            res.setCode(SysParrentUtils.errorCode);
            res.setMessage("请完善信息！");
        }
        return res;
    }

    /**
     * 找回密码
     *
     * @param dto
     * @return
     */
    public RequestDataUtils pawdReset(SysUserDTO dto) {
        RequestDataUtils res = new RequestDataUtils();
        if (!StringUtils.checkStringNotNull(dto.getVfCode())) {
            res.setCode(SysParrentUtils.errorCode);
            res.setMessage("验证码不能为空！");
            return res;
        }
        if (!StringUtils.checkStringNotNull(dto.getPassword())) {
            res.setCode(SysParrentUtils.errorCode);
            res.setMessage("密码不能为空！");
            return res;
        }
        if (!StringUtils.checkStringNotNull(dto.getUserName())) {
            res.setCode(SysParrentUtils.errorCode);
            res.setMessage("手机号不能为空！");
            return res;
        }
        SysUser user = getSysUserByUserName(dto.getUserName());
        if (user != null) {
            //进行修改用户信息
            String Mdpsd = Md5Utils.getResult(dto.getPassword());
            user.setPassword(Mdpsd);
            sysUserMapper.update(user);
            res.setCode(SysParrentUtils.successCode);
            res.setMessage("密码重置完成！");
            return res;
        } else {
            res.setCode(SysParrentUtils.errorCode);
            res.setMessage("账号输入错误！");
            return res;
        }
    }


    public Boolean save(SysUser user) {
        user.setEnable(1);
        user.setCreateTime(new Date());
        user.setUuid(StringUtils.getUUID());
        baseMysqlCRUDManager.save(user);
        return false;
    }
}
